package com.dp.demo.service.api;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.dp.demo.service.entity.Goods;
import com.dp.demo.service.service.GoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: dupeng
 * @CreateTime: 2024-12-05  18:01
 * @Description: TODO
 */
@RestController
@RequestMapping(value = "/api")
@Slf4j
public class ApiTestController {

    @Autowired
    private GoodsService goodsService;

    @GetMapping("/test")
//    @SentinelResource(value = "testResource", blockHandler = "testBlockHandler", blockHandlerClass = ApiTestController.class)
    @SentinelResource(value = "testResource", blockHandler = "testBlockHandler", fallbackClass = ApiTestController.class)
    public String getHello() throws InterruptedException {
//        Thread.sleep(10);
        return "Hello";
    }

    public static String testBlockHandler(BlockException ex) {
        System.out.println("fallback...");
        return "fallback... " ;
    }


    @GetMapping("/remoteSave")
    public void remoteSave(){
        goodsService.save(new Goods(1, "Goods1"));
    }

}
